package com.health.db.service.user.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.health.db.dao.user.IPatientShortInfoDao;
import com.health.db.dao.user.IUserInfoDao;
import com.health.db.dao.user.IUserPatientDao;
import com.health.db.model.PatientShortInfo;
import com.health.db.model.UserInfo;
import com.health.db.model.UserPatient;
import com.health.db.service.user.IPatientShortInfoService;

/**
 * 人员名单表
 * @author YC
 *
 */
@Component("patientShortInfoService")
public class PatientShortInfoServiceImpl implements
		IPatientShortInfoService {

	@Autowired
	IPatientShortInfoDao  patientShortInfoDao;
	
	@Autowired
	IUserPatientDao userPatientDao;
	
	@Autowired
	private IUserInfoDao userInfoDao;

	@Override
	public int insert(PatientShortInfo record,String userId) {
		int executeNum =  patientShortInfoDao.insert(record);
		if (StringUtils.isNotEmpty(userId)) {
			
			//插入绑定信息
			UserPatient userPatient = new UserPatient();
			userPatient.setPatientId(record.getId());
			userPatient.setUserId(Long.valueOf(userId));
			userPatient.setStatus(0);
			
			//修改默认
			UserInfo userInfo = new UserInfo();
			userInfo.setUserId(Long.valueOf(userId));
			userInfo.setDefalutPatientId(record.getId());
			userPatientDao.insert(userPatient);
			userInfoDao.updateByPrimaryKeySelective(userInfo);
		}
		
		// TODO Auto-generated method stub
		return executeNum;
	}

	public IPatientShortInfoDao getPatientShortInfoDao() {
		return patientShortInfoDao;
	}

	public void setPatientShortInfoDao(IPatientShortInfoDao patientShortInfoDao) {
		this.patientShortInfoDao = patientShortInfoDao;
	}

	@Override
	public String selectMedicalCardNoKey() {
		// TODO Auto-generated method stub
		return patientShortInfoDao.selectMedicalCardNoKey();
	}

	@Override
	public int updateByPrimaryKeySelective(PatientShortInfo record) {
		// TODO Auto-generated method stub
		return patientShortInfoDao.updateByPrimaryKeySelective(record);
	}

	public IUserPatientDao getUserPatientDao() {
		return userPatientDao;
	}

	public void setUserPatientDao(IUserPatientDao userPatientDao) {
		this.userPatientDao = userPatientDao;
	}

	@Override
	public List<PatientShortInfo> selectByUserId(Long userId) {
		// TODO Auto-generated method stub
		return patientShortInfoDao.selectByUserId(userId);
	}

	@Override
	public int updateUserPatient(UserPatient userPatient) {
		// TODO Auto-generated method stub
		return userPatientDao.updateByPrimaryKeySelective(userPatient);
	}

	@Override
	public int updateUserPatientByUserIdPatientId(UserPatient record) {
		// TODO Auto-generated method stub
		return userPatientDao.updateByUserIdPatientId(record);
	}

	@Override
	public PatientShortInfo selectByMedicalCardNo(String medicalCardNo) {
		// TODO Auto-generated method stub
		
		List<PatientShortInfo> list = patientShortInfoDao.selectByMedicalCardNo(medicalCardNo);
		if (list.size()>0) {
			return list.get(0);
		}else{
			return null;
		}
		
	}

	@Override
	public PatientShortInfo selectById(Long id) {
		// TODO Auto-generated method stub
		return patientShortInfoDao.selectById(id);
	}

	public IUserInfoDao getUserInfoDao() {
		return userInfoDao;
	}

	public void setUserInfoDao(IUserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}

	
	
	
}
